package com.xf.system.utils.email;

import com.alibaba.excel.EasyExcel;
import com.xf.system.constants.DateConst;
import com.xf.system.response.ApiResult;
import com.xf.system.utils.date.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author xiefengf
 */
@Api(tags = "邮件测试类")
@Slf4j
@RestController
@RequestMapping("/email")
public class EmailController {

    @Resource
    private EmailService emailService;

    List<String> toMails = Arrays.asList("1739482657@qq.com");

    @GetMapping("/sendEmail1")
    @Operation(summary = "发送邮件-不带附件",method = "GET")
    public ApiResult<Boolean> sendEmail1() {
        emailService.sendEmail("测试邮件标题111", "测试邮件内容111", toMails);
        return ApiResult.success(Boolean.TRUE);
    }

    @GetMapping("/sendEmail2")
    @Operation(summary = "发送邮件-带附件",method = "GET")
    public ApiResult<Boolean> sendEmail2() throws IOException {
        // 查询数据库
        // 转为Excel临时文件
        // Excel表头
        ArrayList<List<String>> head = new ArrayList<>();
        //拼装头信息
        head.add(Arrays.asList("表头01"));
        head.add(Arrays.asList("表头02"));
        head.add(Arrays.asList("表头03"));
        ArrayList<List<Object>> dataList = new ArrayList<>();
        dataList.add(Arrays.asList("数据01", "数据02", "数据03"));
        dataList.add(Arrays.asList("数据011", "数据022", "数据033"));
        dataList.add(Arrays.asList("数据0111", "数据0222", "数据0333"));
        File tempFile = File.createTempFile("查询数据_" + DateUtils.dateToString(new Date(), DateConst.SHORT),".xlsx");
        EasyExcel.write(tempFile)
                .head(head)
                .sheet("Sheet 1")
                .doWrite(dataList);

        emailService.sendEmail("测试邮件标题222", "测试邮件内容222", toMails,Arrays.asList(tempFile));
        return ApiResult.success(Boolean.TRUE);
    }

}
